// pages/login/login.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNum: '',
    msg: '',
    hidden: true,
    miao: 60
  },
  subMobel: function(e) {
    this.setData({
      phoneNum: e.detail.value,
    })
  },
  subMobel1: function(e) {
    this.setData({
      msg: e.detail.value,
    })
  },
  login: function() {
    let that = this;
    let tel = that.data.phoneNum;
    let code = that.data.msg;
    let reg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!reg.test(tel)) {
      wx.showModal({
        title: '错误',
        content: '请输入正确的电话',
      })
      return false;
    }

    if (!code) {
      wx.showModal({
        title: '错误',
        content: '请输入验证码',
      })
      return false;
    }

    app.post('/shopuser/login', { tel: tel, code:code}).then(res => {
      wx.reLaunch({
        url: '../authorization/authorization',
      })
    },msg => {
      wx.showModal({
        title: '错误',
        content: msg,
      })
    });
  },

  getmsg: function() {
    let that = this;
    let tel = this.data.phoneNum;
    let reg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!reg.test(tel)) {
      wx.showModal({
        title: '错误',
        content: '请输入正确的电话',
      })
      return false;
    }
    let url = app.globalData.baseUrl + '/shopuser/send_msg';
    wx.request({
      url: url,
      data: {
        tel: tel
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 2000) {
          that.daojishi();
        } else { //返回错误提示信息
          wx.showModal({
            title: '错误',
            content: '该电话号码没有权限登录系统',
          })
        }
      }
    })
  },
  daojishi: function() {
    let that = this;
    this.setData({
      hidden: !that.data.hidden
    })
    let interval = setInterval(function() {
      that.setData({
        miao: --that.data.miao
      });
      if (that.data.miao == -1) {
        clearInterval(interval);
        that.setData({
          hidden: !that.data.hidden,
          miao: 60
        })
      }
    }, 1000)
  }
})